﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Xml.Linq;

namespace Shiny.SmartTravel.Data.Local
{
    /// <summary>
    /// A City entity for local
    /// </summary>
    public class City : INotifyPropertyChanged
    {
        /// <summary>
        /// Empty constructor
        /// </summary>
        public City() { }

        /// <summary>
        /// Gets or sets the unique integer id
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// Gets or sets the province id
        /// </summary>
        public int ProvinceId { get; set; }

        private string name;

        /// <summary>
        /// Gets or sets the name of a City
        /// </summary>
        public string Name
        {
            get { return name; }
            set
            {
                name = value;
                OnPropertyChanged("Name");
            }
        }

        /// <summary>
        /// Gets or sets the uri friendly name
        /// </summary>
        public string UriName { get; set; }

        private string intro;
        /// <summary>
        /// Gets or sets the intro of a City object
        /// </summary>
        public string Intro
        {
            get { return intro; }
            set
            {
                intro = value;
                OnPropertyChanged("Intro");
            }
        }

        /// <summary>
        /// Gets or sets the image uri string
        /// </summary>
        public string Image { get; set; }

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Property changed.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        /// <summary>
        /// Notify
        /// </summary>
        /// <param name="name"></param>
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// Gets the Name of a City
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return this.Name;
        }
    }

    /// <summary>
    /// A city collection
    /// </summary>
    public class CitiesCollection : ObservableCollection<City>
    {
        private string dataFile = "Cities.xml";

        /// <summary>
        /// Initialize cities with specify the province id
        /// </summary>
        /// <param name="provinceId"></param>
        public CitiesCollection(int provinceId)
        {
            try
            {
                IEnumerable<City> cities = LoadCitiesByProvinceId(provinceId);
                foreach (var c in cities)
                {
                    Add(c);
                }
            }
            catch (Exception ex)
            {
                throw new LocalDataException("LocalData Error! Please DO NOT modify the files under LocalData folder, great thanks.", ex);
            }
        }

        private IEnumerable<City> LoadCitiesByProvinceId(int provinceId)
        {
            string path = string.Format("{0}\\{1}", Settings.LocalDataFolder, dataFile);
            XDocument xdoc = XDocument.Load(path);
            IEnumerable<City> cities = from c in xdoc.Element("XutourCities").Elements("City")
                                       where (int)c.Element("ProvinceId") == provinceId
                                       select new City { Id = (int)c.Element("Id"), ProvinceId=(int)c.Element("ProvinceId"), Name = (string)c.Element("Name"), Image = (string)c.Element("Image"), Intro = (string)c.Element("Intro"), UriName=(string)c.Element("UriName") };
            return cities;
        }
    }
}
